// JavaScript Document

$(document).ready(function()
{

});

function GetGoodsInfo(type)
{
	var params = {"rCode": WMALL000004};
	
	if (!isInvalid(type))
	{
		params.gdsBigType = type;
	}
	
	GetRequestData(params, GoodsInfoCallback);		
}

function GoodsInfoCallback(data)
{
	if (data[JSON_KEY_CODE] == RESPOND_CODE_SUCCESS)
	{
		DisplayGoodsInfo(data.infos);
	}
	else if (data[JSON_KEY_CODE] == RESPOND_CODE_RECORD_NOT_EXIST)
	{
		
	}
	else if (data[JSON_KEY_CODE] == RESPOND_CODE_AJAX_FAILURE)
	{
	}
	else
	{
	}
}

function DisplayGoodsInfo(data)
{
	var _html = '<tr>';
	var template = 
			'<td>' +
				'<div class="goods_item">' +
					'<img src="{0}">' +
					'<a>{1}</a>' +
					'<a class="goods_price">￥ <strong>{2}</strong></a>' +
					'<div><img src="image/cart.png"></div>' +
					'<input type="hidden" value="{3}" class="goods_id" />' +
					'<input type="hidden" value="{4}" class="goods_left" />' + 
				'</div>' +
			'</td>';
	
	
	for (var i=0; i<data.length; i++)
	{
		if (i % 3 == 0 && i != 0)
		{
			_html += '</tr><tr>';
		}

		_html += template.format(URL_IMAGE + data[i].gdsImgUrl, data[i].gdsName, data[i].gdsSPric.toFixed(2), data[i].gdsNo, data[i].gdsLeft);
		//_html += template.format(URL_IMAGE + data[i].gdsImgUrl, data[i].gdsName, 2.5, 'g00001', 22);
	}
	
	if (data.length < 3)
	{
		for (var i=0; i<3-data.length; i++)
		{
			_html += '<td class="empty"></td>';
		}
	}
	
	_html += '</tr>';
	
	$('#goods_list').find('tbody').html(_html);		
	
	$('.goods_item').children('a').width(window.screen.width/3 - 20);
	
	var _width = $('.goods_item').children('img').width();	
	
	$('.goods_item').children('img').height(_width);	
	
	InitFly();
}

function InitFly()
{
	var offset = $("#circle_cart_count").offset(); 
	
    $(".goods_item").click(function(event){ 
        var addcar = $(this); 
        var img = addcar.children('img').attr('src'); 
		var goodsId = addcar.find('.goods_id').val();
		var leftCount = parseInt(addcar.find('.goods_left').val());
        var flyer = $('<img class="u-flyer" src="'+img+'">'); 
        flyer.fly({ 
            start: { 
                left: event.clientX, //开始位置（必填）#fly元素会被设置成position: fixed 
                top: event.clientY //开始位置（必填） 
            }, 
            end: {
				 
                left: offset.left, //结束位置（必填） 
                top: offset.top, //结束位置（必填） 
                width: 4, //结束时宽度 
                height: 4 //结束时高度
				 
            }, 
            onEnd: function(){ //结束回调 
				AddToCart(goodsId, 1, leftCount);
				
				$("#circle_cart_count").html(GetCartCount());
				
                this.destroy() //移除dom 
            } 
        }); 
    }); 
}



